package com.lx.video.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lx.bilibili.entity.Danmu;
import com.lx.bilibili.entity.Video;
import com.lx.bilibili.entity.VideoDataDetail;
import com.lx.video.dao.VideoDataDetailMapper;
import com.lx.video.dao.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;

@RestController
@RequestMapping("VideoDataDetail")
public class VideoDataDetailController {

    @Autowired
    VideoDataDetailMapper videoDataDetailMapper;

    @Autowired
    VideoMapper videoMapper;

    @Resource
    private RedisTemplate<String, Danmu> redisTemplate;

    @RequestMapping("savelike-coin-collect")
    public void saveLikeCoinCollect(@RequestBody VideoDataDetail videoDataDetail) {
        // 更新详情表
        System.out.println("videoDataDetail = " + videoDataDetail);
        if(videoDataDetail == null && videoDataDetail.getUid() != null){
            return ;
        }
        videoDataDetail.setTime(new Timestamp(System.currentTimeMillis()));
        LambdaQueryWrapper<VideoDataDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoDataDetail::getVid, videoDataDetail.getVid());
        lqw.eq(VideoDataDetail::getUid, videoDataDetail.getUid());
        VideoDataDetail videoDataDetail1 = videoDataDetailMapper.selectOne(lqw);
        if(videoDataDetail1 != null){
            // 存在这条信息，更新
            videoDataDetail.setId(videoDataDetail1.getId());
            videoDataDetailMapper.updateById(videoDataDetail);
            this.reStoreVideoDataDetail(videoDataDetail, videoDataDetail1);
        }else{
            // 不存在，插入
            videoDataDetailMapper.insert(videoDataDetail);
            LambdaUpdateWrapper<Video> lqw2 = new LambdaUpdateWrapper<>();
            lqw2.eq(Video::getId, videoDataDetail.getVid()).setSql("likes = likes + 1 , collect = collect + 1 , coin =  coin + 1 ");
            videoMapper.update(lqw2);
        }

    }

    @RequestMapping("getDataByUidAndVid")
    public VideoDataDetail getDataByUidAndVid(int uid, int vid) {
        LambdaQueryWrapper<VideoDataDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoDataDetail::getUid, uid);
        lqw.eq(VideoDataDetail::getVid, vid);
        return videoDataDetailMapper.selectOne(lqw);
    }




    public void reStoreVideoDataDetail(VideoDataDetail videoDataDetail, VideoDataDetail videoDataDetail1){
        // 更新视频表
        if(videoDataDetail.getLikes() != videoDataDetail1.getLikes()){
            LambdaUpdateWrapper<Video> luw = new LambdaUpdateWrapper<>();
            if(videoDataDetail.getLikes() == 1){
                luw.eq(Video::getId, videoDataDetail.getVid()).setSql("likes = likes+1");
                videoMapper.update(luw);
            }else{
                luw.eq(Video::getId, videoDataDetail.getVid()).setSql("likes = likes-1");
                videoMapper.update(luw);
            }
        }
        if(videoDataDetail.getCoin() != videoDataDetail1.getCoin()){
            LambdaUpdateWrapper<Video> luw = new LambdaUpdateWrapper<>();
            if(videoDataDetail.getCoin() == 1){
                luw.eq(Video::getId, videoDataDetail.getVid()).setSql("coin = coin+1");
                videoMapper.update(luw);
            }else{
                luw.eq(Video::getId, videoDataDetail.getVid()).setSql("coin = coin-1");
                videoMapper.update(luw);
            }
        }
        if(videoDataDetail.getCollect() != videoDataDetail1.getCollect()){
            LambdaUpdateWrapper<Video> luw = new LambdaUpdateWrapper<>();
            if(videoDataDetail.getCollect() == 1){
                luw.eq(Video::getId, videoDataDetail.getVid()).setSql("collect = collect+1");
                videoMapper.update(luw);
            }else{
                luw.eq(Video::getId, videoDataDetail.getVid()).setSql("collect = collect-1");
                videoMapper.update(luw);
            }
        }
    }
}
